package com.example.es.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;

/**
 * @description:  multi_match query
 * match 针对一个field 做检索，multi_math 针对多个field 进行检索，多个field对应一个文本
 * @author: ruoan
 * @date: 2020/10/11 11:57
 */
public class EsRestHighLevelClientTest11 {

    private RestHighLevelClient client= EsRestHighLevelClient.getClient();
    ObjectMapper mapper = new ObjectMapper();
    String index = "sms-logs-index";
    String type="sms-logs-type";

    @Test
    public void multiMatch() throws IOException {


        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(index).types(type);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(12);
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery("晋城","smsContent province")
                .operator(Operator.AND));
        searchRequest.source(searchSourceBuilder);


        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }







}